package com.mission.schedule.activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.Request.Method;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.mission.schedule.R;
import com.mission.schedule.applcation.App;
import com.mission.schedule.constants.ShareFile;
import com.mission.schedule.constants.URLConstants;
import com.mission.schedule.utils.GetDeviceId;
import com.mission.schedule.utils.ProgressUtil;
import com.mission.schedule.utils.SharedPrefUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CodeActivity extends BaseActivity {

    SharedPrefUtil sharedPrefUtil = null;
    ProgressUtil progressUtil;
    String code = "";

    @Override
    protected void setListener() {

    }

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_code);
        sharedPrefUtil = new SharedPrefUtil(this, ShareFile.USERFILE);
        String userName = sharedPrefUtil.getString(this, ShareFile.USERFILE, ShareFile.USERNAME, "");
        ((TextView) findViewById(R.id.name)).setText("是昵称为"+userName+"的安全码");
        progressUtil = new ProgressUtil();
        progressUtil.ShowProgress(this, true, true, "获取安全码...");

        new Thread(() -> {

            StringRequest stringRequest = new StringRequest(Request.Method.POST, URLConstants.HOST + "/user_findTbUserUnionNumber.do",
                    result -> {
                        progressUtil.dismiss();
                        Gson gson = new Gson();
                        JB jb = gson.fromJson(result, JB.class);
                        if (jb.status == 0) {
                            if (jb.list != null && jb.list.size() > 0) {
                                code = jb.list.get(0).unionNumber+"";
                                handler.sendEmptyMessage(0);
                            }
                        } else {
                            Toast.makeText(CodeActivity.this, "网络异常,请重新获取安全码", Toast.LENGTH_SHORT).show();
                            finish();
                        }
                    }, volleyError -> {
                        progressUtil.dismiss();
                        Toast.makeText(CodeActivity.this, "网络异常,请重新获取安全码", Toast.LENGTH_SHORT).show();
                        finish();
                    }) {
                @Override
                protected Map<String, String> getParams() {
                    Map<String, String> map = new HashMap<>();
                    map.put("uname", userName);
                    map.put("data", "TimeSheet_A_"+GetDeviceId.getDeviceId(CodeActivity.this).replaceAll(":","0").replaceAll("%", "1"));
                    return map;
                }
            };
            stringRequest.setTag("downUserNewLy");
            stringRequest.setRetryPolicy(new DefaultRetryPolicy(100000, 1, 1.0f));
            App.queues.add(stringRequest);
        }).start();

        findViewById(R.id.bacl_ll).setOnClickListener(v -> finish());
    }

    @SuppressLint("HandlerLeak")
    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            ((TextView)findViewById(R.id.code)).setText(code);
        }
    };

    @Override
    protected void init(Bundle savedInstanceState) {

    }

    @Override
    protected void setAdapter() {

    }


    class JB {

        /**
         * downTime : 2019-04-16T11:22:30
         * list : [{"address":"TimeSheet_A_da488a4adde29747565392309e3f58ef","uname":"user1654423s","unionNumber":137339,"uId":1654423,"utype":1}]
         * list2 : null
         * listOpdata : null
         * message : 成功
         * page : null
         * status : 0
         * urlMess : null
         */

        private String downTime;
        private Object list2;
        private Object listOpdata;
        private String message;
        private Object page;
        private int status;
        private Object urlMess;
        private List<ListBean> list;

        public String getDownTime() {
            return downTime;
        }

        public void setDownTime(String downTime) {
            this.downTime = downTime;
        }

        public Object getList2() {
            return list2;
        }

        public void setList2(Object list2) {
            this.list2 = list2;
        }

        public Object getListOpdata() {
            return listOpdata;
        }

        public void setListOpdata(Object listOpdata) {
            this.listOpdata = listOpdata;
        }

        public String getMessage() {
            return message;
        }

        public void setMessage(String message) {
            this.message = message;
        }

        public Object getPage() {
            return page;
        }

        public void setPage(Object page) {
            this.page = page;
        }

        public int getStatus() {
            return status;
        }

        public void setStatus(int status) {
            this.status = status;
        }

        public Object getUrlMess() {
            return urlMess;
        }

        public void setUrlMess(Object urlMess) {
            this.urlMess = urlMess;
        }

        public List<ListBean> getList() {
            return list;
        }

        public void setList(List<ListBean> list) {
            this.list = list;
        }

        public class ListBean {
            /**
             * address : TimeSheet_A_da488a4adde29747565392309e3f58ef
             * uname : user1654423s
             * unionNumber : 137339
             * uId : 1654423
             * utype : 1
             */

            private String address;
            private String uname;
            private int unionNumber;
            private int uId;
            private int utype;

            public String getAddress() {
                return address;
            }

            public void setAddress(String address) {
                this.address = address;
            }

            public String getUname() {
                return uname;
            }

            public void setUname(String uname) {
                this.uname = uname;
            }

            public int getUnionNumber() {
                return unionNumber;
            }

            public void setUnionNumber(int unionNumber) {
                this.unionNumber = unionNumber;
            }

            public int getUId() {
                return uId;
            }

            public void setUId(int uId) {
                this.uId = uId;
            }

            public int getUtype() {
                return utype;
            }

            public void setUtype(int utype) {
                this.utype = utype;
            }
        }
    }
}
